package com.onepiece.shipelves.common.context;

/**
 * @author guoyanjiang
 * @date 2018/7/9
 */
public class BaseWebContext {

    public static ThreadLocal<Info> context = new ThreadLocal<>();

    public static void destroy() {
        context.remove();
    }

    static class Info {
        String ip;
        String from;
        String userAgent;
        String uid;
        String productName;
        String deviceId;

        public Info() {
        }

        public Info(String ip, String from, String userAgent, String uid, String productName, String deviceId) {
            this.ip = ip;
            this.from = from;
            this.userAgent = userAgent;
            this.uid = uid;
            this.productName = productName;
            this.deviceId = deviceId;
        }
    }

    public static void initInfo(String ip, String from, String userAgent, String uid, String productName, String deviceId) {
        context.set(new Info(ip, from, userAgent, uid, productName, deviceId));
    }

    public static String getIP() {
        Info info = context.get();
        return info == null ? null : info.ip;
    }

    public static void setIP(String ip) {
        if (context.get() == null) {
            context.set(new Info());
        }
        context.get().ip = ip;
    }

    public static String getFrom() {
        Info info = context.get();
        return info == null ? "java" : info.from;
    }

    public static void setFrom(String from) {
        if (context.get() == null) {
            context.set(new Info());
        }
        context.get().from = from;
    }

    public static String getUserAgent() {
        Info info = context.get();
        return info == null ? null : info.userAgent;
    }

    public static String getUid() {
        Info info = context.get();
        return info == null ? null : info.uid;
    }

    public static String getProductName() {
        Info info = context.get();
        return info == null ? null : info.productName;
    }

    public static String getDeviceId() {
        Info info = context.get();
        return info == null ? null : info.deviceId;
    }

}
